using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Windows.Forms;
using System.IO;

namespace HubLog
{
    class GlobalConfig
    {
        public Hashtable columnTitle;
        public Hashtable columnWidth;
        public String[] AutoRuns;
        private String configName;
        private XPathDocument xdoc;
        private XPathNavigator xpn;

        public GlobalConfig()
        {
            columnTitle = null;
            columnWidth = null;
            AutoRuns = null;
            configName = Path.GetFileName(Application.ExecutablePath) + ".Xml";
            xdoc = null;
            xpn = null;
        }

        public string ConfigFileName { get { return this.configName; } }

        public void Initialize()
        {
            xdoc = null;
            xdoc = new XPathDocument(new XmlTextReader(configName), XmlSpace.Preserve);
            xpn = xdoc.CreateNavigator();
            //
            initializeColumns();
            initializeAutoRuns();
            //
            return ;
        }
        private void initializeColumns()
        {
            columnTitle = new Hashtable();
            columnWidth = new Hashtable();
            XPathNodeIterator xni = null;
            xni = xpn.Select("/Config/Global/Columns/*");
            System.Diagnostics.Debug.WriteLine(string.Format("xni.Count-->{0}", xni.Count));
            if (0 == xni.Count)
            {
                return ;
            }
            string columnName;
            while (xni.MoveNext())
            {
                XPathNavigator xnav2 = xni.Current.Clone();
                columnName = xnav2.Name;
                if (xnav2.HasAttributes)
                {
                    xnav2.MoveToFirstAttribute();
                    do{
                        System.Diagnostics.Debug.WriteLine(string.Format("{0}/{1}", xnav2.Name, xnav2.Value));
                        string value = xnav2.Value.Trim();
                        switch (xnav2.Name)
                        {
                            case "title":
                                columnTitle.Add(columnName, value);
                                break;
                            case "width":
                                columnWidth.Add(columnName, value);
                                break;
                        }
                    } while (xnav2.MoveToNextAttribute());
                }
            }
            //
            return ;
        }
        private void initializeAutoRuns()
        {
            XPathNodeIterator xni = null;
            xni = xpn.Select("/Config/Global/AutoRuns/*");
            System.Diagnostics.Debug.WriteLine(string.Format("xni.Count-->{0}", xni.Count));
            if (0 == xni.Count)
            {
                return;
            }
            int n = 0;
            while (xni.MoveNext())
            {
                XPathNavigator xnav2 = xni.Current.Clone();
                if ("Run" == xnav2.Name)
                {
                    if (null == this.AutoRuns)
                    {
                        this.AutoRuns = new String[xni.Count];
                    }
                    this.AutoRuns[n] = xnav2.Value.Trim();
                    n++;
                }
            }
            return;
        }

    }//class
}//namespace
